import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

import '../../commont/base_group_grid.dart';
import '../../commont/button.dart';
import '../../model/model.dart';
import '../../model/model_default.dart';
import '../../tool/assets.dart';

class GroupView extends BaseGroupGrid {
  bool? enable;
  void Function(GroupModel) onLongPress;

  GroupView({
    super.key,
    required super.list,
    required super.onClick,
    required this.onLongPress,
    this.enable,
  }) : super();

  @override
  State<GroupView> createState() => _GroupViewState();
}

class _GroupViewState extends BaseGroupGridState<GroupView> {
  @override
  List<List<Widget>> buildContent() {
    List<GroupModel> list = widget.list.first;
    return [
      list.map(($0) {
        return GroupItem(group: $0, enable: widget.enable)
          ..onClick = (group) {
            widget.onClick(group);
          }
          ..onLongPress = (group) {
            widget.onLongPress(group);
          };
      }).toList()
    ];
  }
}

class GroupItem extends StatefulWidget {
  bool? enable;
  GroupModel group;

  void Function(GroupModel)? onClick;
  void Function(GroupModel)? onLongPress;
  GroupItem({super.key, required this.group, this.enable});

  @override
  State<GroupItem> createState() => _GroupItemState();
}

class _GroupItemState extends State<GroupItem> {
  late final LongPressGestureRecognizer _longPressRecognizer;
  bool get enable {
    return widget.enable ?? globalModel.enable && widget.group.enable;
  }

  @override
  void initState() {
    _longPressRecognizer =
        LongPressGestureRecognizer(duration: const Duration(milliseconds: 2000))
          ..onLongPress = () {
            if (widget.onLongPress != null) {
              widget.onLongPress!(widget.group);
            }
          };
    super.initState();
  }

  @override
  void dispose() {
    _longPressRecognizer.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return RawGestureDetector(
      gestures: {
        LongPressGestureRecognizer:
            GestureRecognizerFactoryWithHandlers<LongPressGestureRecognizer>(
          () => _longPressRecognizer,
          (LongPressGestureRecognizer instance) {},
        )
      },
      behavior: HitTestBehavior.opaque,
      child: kTextButton(
        name: widget.group.name,
        fontSize: 14,
        line: 2,
        select: widget.group.select,
        enable: enable,
        textColor: enable == false
            ? Color_disEnable
            : widget.group.select
                ? Colors.white
                : Colors.black,
        borderColor: enable == false
            ? Color_disEnable
            : widget.group.select
                ? Color_theme
                : Colors.black,
        groundColor: widget.group.select ? Color_theme : Color_bg_view,
        onClick: widget.group.name.contains('CH') && widget.group.select
            ? null
            : (p0) {
                if (widget.onClick != null) {
                  widget.onClick!(widget.group);
                }
              },
      ),
    );
  }
}
